package org.jiuling.wechat.officialaccount.service.dialogue;

import org.jiuling.wechat.officialaccount.exception.WeChatOfficialAccountException;
import org.jiuling.wechat.officialaccount.model.ReceiveMessageModel;
import org.jiuling.xml.jaxb.XmlConverter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.StringReader;

/**
 * 接受消息
 * @author 紫灵
 * @date 2024年11月27日 22时54分47秒
 */
public class ReceiveMessage {

    /**
     * 读取消息
     * @param xmlMsg 接收到的消息
     * @return 接收消息模型
     * @throws WeChatOfficialAccountException 微信公众号异常
     */
    public ReceiveMessageModel readMsg(String xmlMsg) throws WeChatOfficialAccountException {
        System.out.println("xmlMsg:"+xmlMsg);
        try {
            return XmlConverter.toObject(xmlMsg, ReceiveMessageModel.class);
        } catch (JAXBException e) {
            throw new WeChatOfficialAccountException("XML数据异常，无法转换！");
        }
    }

}
